import * as vscode from 'vscode';
import axios from 'axios';

/**
 *
 * @param email 邮箱
 * @param password 密码
 */
export function loginApi(email:String,password:String){
    // 使用promise同步返回
    return new Promise((resolve, reject)=>{
        try {
            axios.post('https://alg.zmengine.com/alg/login', {
                user: email,
                pwd: password,
                type: 0 // 插件登录，不验证IP
            }).then(response => {
                const data = response.data;

                if (data.success) {
                    resolve(data.data);
                } else {
                    vscode.window.showInformationMessage(data.msg);
                    reject(false);
                }
            });
        } catch (error) {
            console.error('登录失败:', error);
            vscode.window.showInformationMessage('登录失败，请检查网络连接或稍后再试。');
            reject(false);
        }
    });
}

/**
 *
 * @param token 验证登录的token
 * @returns
 */
export function isLoginApi(token: String){
    // 使用promise同步返回
    return new Promise((resolve, reject)=>{
        try{
                axios.post('https://alg.zmengine.com/alg/isLogin', {
                    token: token
                }).then(response => {
                    const data = response.data;

                    if (data.success) {
                        console.log('登录有效');
                        resolve(true);
                    } else {
                        console.log('登录无效');
                        reject(false);
                    }
                });
        } catch(error){
            console.error('验证登录失败:', error);
            reject(false);
        }
    });
}
